
package com.ams.util;

import java.util.ArrayList;
import java.util.List;


public class Pageable 
{
	private int pageCount;      //total pages
	private int pageSize;      //Number of records per page（When the PageSize = 0 indicates that the page displays all the records ）
	private int recordCount;   //The total number of records
	private int curPage=1;     //The Current Page
	private List<?> list;  

	
	 /**
	 * constructed method
	 * @param rs   To deal with the result set
	 * @param pageSize  page size
	 * 
	 */
	public Pageable(int pageSize)
	{
		setPageSize(pageSize);
	}
	

	public void init(List<?> list)
	{
		this.list=list;
		if(list==null){
			list=new ArrayList<Object>();
			this.recordCount=0;
		}else{
			this.recordCount=list.size();
		}
		getPageCount();          //获取总页数
	}
	
	public int getPageCount()
	{
		if(recordCount==0) 
		{
			return pageCount=0; 
		}
			  
		if(pageSize==0) 
		{
			return pageCount=1;     //pageSize=0表示将所有的记录在一个页面中显示
		}
			
		if(recordCount%pageSize==0)
		{
			 pageCount=recordCount/pageSize;
		}else
		{//When recording cannot be divided, explaining the last page of the number of records is less than the PageSize
			pageCount=recordCount/pageSize+1;
		}
		return pageCount;
	}

	/**
	 * Return all data of the specified page.
	 * @param page
	 * @return
	 */
	public List<?> gotoPage(int page)
	{
		if(pageCount==0)
		{
			return list.subList(0, 0);
		}
		if(page>pageCount)
		{
			curPage=pageCount;
		}else if(page<1)
		{
			curPage=1;
		}else
		{
			curPage=page;
		}
		int startRowIndex=(curPage-1)*pageSize; //Page pointer to the current First Record and startRowIndex>=0 
		int endRowIndex=startRowIndex+returnCurrentPageRecordCount();
		return list.subList(startRowIndex, endRowIndex);//The range don't include the endRowIndex.
	}
	
	/**
	 * Set the number of records per page
	 * @param pageSize
	 */
	public void setPageSize(int pageSize)
	{
		this.pageSize=pageSize;
	}
    
	
	/**
	 * Under the final for a record number of rows
	 */
	public int returnCurPageLastRow() 
	{
		return (curPage-1)*pageSize+returnCurrentPageRecordCount();
	}

	
	/**
	 * Returns the number of records of the current page
	 */
	public int returnCurrentPageRecordCount()
	{
		if(pageSize==0)
		{  //When all the records only when a page is displayed
			return getRecordCount();
		}else if(recordCount==0)
		{ //The result set is not recorded
			return 0;
		}else if(curPage!=pageCount)
		{ //The current page is not the last page
			return getPageSize();
		}else 
		{   //The current page is the last page(The number of records that may last less than the PageSize)
			return recordCount-pageSize*(curPage-1);
		}
	}

	
	/**
	 *Returns the size of the page
	 *@return
	 */
	public int getPageSize()
	{
		return pageSize;
	}
	

	/**
	 *Returns the current page
	 *@return
	 */
	public int getCurPage()
	{
		return curPage;
	}
	
	
	/**
	 *return total records
	 *@return
	 */
	public int getRecordCount()
	{
		return recordCount;
	}
	
	/**
	 *Returns the current page the page code value
	 */
	public void setCurPage(int curPage) 
	{
		if(curPage>0)
		{
			this.curPage = curPage;
		}
		
	}

	
}










